package main

import (
	"fmt"
	"log"
	"net"
	"time"
)

// 参考:  https://studygolang.com/articles/5372
func main() {
	var msg = []byte("acd")
	//conn,err := net.Dial("tcp","127.0.0.1:8080")
	conn, err := net.DialTimeout("tcp", "127.0.0.1:8080", 2*time.Second)
	if err != nil {
		panic(err)
	}
	// 发送消息
	n, err := conn.Write([]byte(msg))
	if err != nil {
		panic(err)
	}
	fmt.Println("发送消息:" + string(msg))

	// 读取消息
	buf := make([]byte, 100)
	n, err = conn.Read(buf)
	if err != nil {
		log.Println(n, err)
		panic(err)
	}
	fmt.Println("收到消息: " + string(buf[:n]))
}
